<%@ page language="java" pageEncoding="UTF-8" import="
com.wlanauth.core.criteria.GeneralCriteria,
com.wlanauth.domain.radius.RadAcct,
com.wlanauth.domain.radius.RadCheck,
org.hibernate.criterion.Projections,
com.wlanauth.core.criteria.GeneralCriteriaResult,
java.util.*,
com.wlanauth.vo.UserVO,
com.wlanauth.web.util.RadiusUtils,
org.apache.commons.logging.Log,
org.apache.commons.logging.LogFactory,
com.wlanauth.domain.manage.User,org.hibernate.criterion.Restrictions"%>
<%!
//此页面不在action写
org.apache.commons.logging.Log logger = LogFactory.getLog(com.wlanauth.web.action.RadiusUserAction.class);

void responseClient(java.io.PrintWriter writer,String code,String msg,boolean success){
	
	String jsonStr = "var response={success:"+success+",code:'"+code+"',msg:'"+msg+"'};";
	writer.write(jsonStr);
	logger.info("======= 完成 接入认证["+jsonStr+"]");
}
String getPwd(String ip){
	//调用通用查询，查询用户
	GeneralCriteria criteria = new GeneralCriteria(RadCheck.class);
	criteria.add(Restrictions.eq("attribute", "User-Password"));
	criteria.add(Restrictions.eq("username", ip));

	GeneralCriteriaResult<RadCheck> result = criteria.executeQuery();
	
	if(result.getData() == null||result.getData().size()<1){
		return "wlanauthpwd";//默认密码
	}
	return result.getData().get(0).getValue();
	
}
%>
<%
try {
	String ip = request.getParameter("ip");
	logger.info("======= 开始 接入认证[ip:"+ip+",localAddr:"+request.getLocalAddr()+",remoteAddr:"+request.getRemoteAddr()+"]");
	//调用通用查询，查询用户
	GeneralCriteria userCriteria= new GeneralCriteria(UserVO.class);
	userCriteria.add(Restrictions.eq("bindIP", ip));
	
	GeneralCriteriaResult<UserVO> userResult = userCriteria.executeQuery();
	if(userResult.getData()== null || userResult.getData().size()<1){
		responseClient(response.getWriter(),"ERROR_IP","未查询到绑定该IP的用户(IP:"+ip+")",false);
		return ;
	} 
	if(userResult.getData().size()>1){
		responseClient(response.getWriter(),"ERROR_SYSDATA","系统数据错误:数据超出额定数目(IP:"+ip+")",false);
	    return ;
	}
	UserVO user = userResult.getData().get(0);
	if(!User.STATUS_ENABLED.equals(user.getStatus())){
		responseClient(response.getWriter(),"ERROR_USERSTATUS","该用户被禁用",false);
	    return ;
	}
	Double sumOctets = RadiusUtils.getViewOct(user.getSumOctets()).doubleValue();
	if(user.getMaxOctets().longValue()*1024*1024>sumOctets){
		responseClient(response.getWriter(),null,getPwd(ip),true);
	} else {
		responseClient(response.getWriter(),"ERROR_NONEOCT","流量超出额定流量限制",false);
	}
	
} catch (Exception e){
	responseClient(response.getWriter(),"ERROR_SYS","系统错误:"+e.getMessage(),false);
}
%>



